<template>
  <view class="tutor-detail-container">
    <!-- 加载中状态 -->
    <view class="loading-state" v-if="loading">
      <text>加载中...</text>
    </view>
    
    <!-- 错误状态 -->
    <view class="error-state" v-if="error">
      <text>{{ error }}</text>
    </view>
    
    <!-- 详情内容 -->
    <view class="detail-content" v-if="!loading && !error && tutorDetail">
      <!-- 基本信息 -->
      <view class="basic-info">
        <view class="tutor-name">{{ tutorDetail.name }}</view>
        <view class="tutor-stars">
          <text class="star-icon" v-for="i in 5" :key="i">
            {{ i <= tutorDetail.stars ? '★' : '☆' }}
          </text>
        </view>
        <view class="tutor-basic">
          <text>{{ tutorDetail.gender === 1 ? '男' : '女' }}</text>
          <text>{{ tutorDetail.age }}岁</text>
          <text>{{ tutorDetail.weight }}kg</text>
          <text>{{ tutorDetail.height }}cm</text>
          <text>￥{{ tutorDetail.price }}/小时</text>
        </view>
      </view>
      
      <!-- 详细信息列表 -->
      <view class="info-list">
        <view class="info-item">
          <text class="info-label">状态：</text>
          <text class="info-value">{{ getStatusText(tutorDetail.status) }}</text>
        </view>
        <view class="info-item">
          <text class="info-label">毕业院校：</text>
          <text class="info-value">{{ tutorDetail.school || '暂无' }}</text>
        </view>
        <view class="info-item">
          <text class="info-label">籍贯：</text>
          <text class="info-value">{{ tutorDetail.birthplace || '暂无' }}</text>
        </view>
        <view class="info-item">
          <text class="info-label">自我介绍：</text>
          <text class="info-value">{{ tutorDetail.content || '暂无' }}</text>
        </view>
        <view class="info-item">
          <text class="info-label">创建时间：</text>
          <text class="info-value">{{ tutorDetail.createtime }}</text>
        </view>
        <view class="info-item">
          <text class="info-label">更新时间：</text>
          <text class="info-value">{{ tutorDetail.updatetime }}</text>
        </view>
      </view>
      
      <!-- 操作按钮 -->
      <view class="action-buttons">
        <button class="action-btn edit-btn" @click="editTutor">编辑</button>
        <button class="action-btn delete-btn" @click="deleteTutor">删除</button>
        <button class="action-btn back-btn" @click="back">返回</button>
      </view>
    </view>
  </view>
</template>

<script>
export default {
  data() {
    return {
      tutorDetail: null,
      loading: false,
      error: ''
    };
  },
  onLoad(options) {
    if (options.id) {
      this.getTutorDetail(options.id);
    } else {
      this.error = '参数错误，无法获取助教详情';
    }
  },
  methods: {
    // 获取助教详情
    async getTutorDetail(id) {
      try {
        this.loading = true;
        this.error = '';
        
        const res = await this.$request({
          url: '/tutor/detail',
          method: 'GET',
          params: {
            ids: id
          }
        });
        
        if (res.code === 1) {
          this.tutorDetail = res.data;
        } else {
          throw new Error(res.msg || '获取助教详情失败');
        }
      } catch (error) {
        console.error('获取助教详情失败:', error);
        this.error = error.message || '获取助教详情失败';
        this.tutorDetail = null;
      } finally {
        this.loading = false;
      }
    },
    
    // 获取状态文本
    getStatusText(status) {
      const statusMap = {
        'busy': '忙碌',
        'free': '空闲',
        'offline': '离线'
      };
      return statusMap[status] || status;
    },
    
    // 编辑助教
    editTutor() {
      uni.navigateTo({
        url: `/pages/tutor/components/tutoredit?id=${this.tutorDetail.id}`
      });
    },
    
    // 删除助教
    deleteTutor() {
      uni.showModal({
        title: '提示',
        content: '确定要删除该助教吗？',
        success: async (res) => {
          if (res.confirm) {
            try {
              const result = await this.$request({
                url: '/tutor/delete',
                method: 'POST',
                data: {
                  ids: this.tutorDetail.id
                }
              });
              
              if (result.code === 1) {
                uni.showToast({
                  title: '删除成功',
                  icon: 'success'
                });
                // 返回上一页
                setTimeout(() => {
                  uni.navigateBack();
                }, 1500);
              } else {
                throw new Error(result.msg || '删除失败');
              }
            } catch (error) {
              console.error('删除助教失败:', error);
              uni.showToast({
                title: error.message || '删除失败',
                icon: 'none'
              });
            }
          }
        }
      });
    },
    
    // 返回上一页
    back() {
      uni.navigateBack();
    }
  }
};
</script>

<style scoped>
.tutor-detail-container {
  min-height: 100vh;
  background-color: #f8f8f8;
  padding: 20rpx;
}

.loading-state,
.error-state {
  text-align: center;
  padding: 100rpx 0;
  color: #999;
  font-size: 28rpx;
}

.error-state {
  color: #ff3b30;
}

.detail-content {
  background-color: #fff;
  border-radius: 15rpx;
  padding: 30rpx;
  box-shadow: 0 2rpx 10rpx rgba(0, 0, 0, 0.05);
}

.basic-info {
  text-align: center;
  margin-bottom: 40rpx;
  padding-bottom: 30rpx;
  border-bottom: 1rpx solid #eee;
}

.tutor-name {
  font-size: 36rpx;
  font-weight: bold;
  color: #333;
  margin-bottom: 15rpx;
}

.tutor-stars {
  display: flex;
  justify-content: center;
  gap: 5rpx;
  margin-bottom: 15rpx;
}

.star-icon {
  font-size: 28rpx;
  color: #ffcc00;
}

.tutor-basic {
  display: flex;
  justify-content: center;
  gap: 20rpx;
  flex-wrap: wrap;
}

.tutor-basic text {
  font-size: 24rpx;
  color: #666;
  background-color: #f5f5f5;
  padding: 5rpx 15rpx;
  border-radius: 15rpx;
}

.info-list {
  margin-bottom: 40rpx;
}

.info-item {
  display: flex;
  margin-bottom: 25rpx;
  align-items: flex-start;
}

.info-item:last-child {
  margin-bottom: 0;
}

.info-label {
  font-size: 28rpx;
  color: #333;
  font-weight: bold;
  width: 140rpx;
  flex-shrink: 0;
}

.info-value {
  font-size: 28rpx;
  color: #666;
  flex: 1;
  word-break: break-word;
  line-height: 1.5;
}

.action-buttons {
  display: flex;
  gap: 20rpx;
}

.action-btn {
  flex: 1;
  height: 90rpx;
  line-height: 90rpx;
  font-size: 32rpx;
  border-radius: 8rpx;
  margin: 0;
}

.edit-btn {
  background-color: #007aff;
  color: #fff;
  border: none;
}

.delete-btn {
  background-color: #ff3b30;
  color: #fff;
  border: none;
}

.back-btn {
  background-color: #f5f5f5;
  color: #333;
  border: 1rpx solid #ddd;
}
</style>